package com.gzhryc.shared_device.cdb.code.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.cdb.code.dao.db.BatteryCabinetSlot;
import com.gzhryc.shared_device.cdb.code.dao.BatteryCabinetSlotDao;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BatteryCabinetSlotService {

    static Logger log = Logger.getLogger(BatteryCabinetSlotService.class);

    public static BatteryCabinetSlotService self() {
        return MultiDBTools.getService(BatteryCabinetSlotService.class, true);
    }

    public static BatteryCabinetSlotService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, BatteryCabinetSlotService.class, true);
    }

    BatteryCabinetSlotDao dao;

    public BatteryCabinetSlotService(String jdbcKey) {
        dao = new BatteryCabinetSlotDao(jdbcKey);
    }

    public List<BatteryCabinetSlot> findJoinByDeviceSn(String deviceSn){
        if(StringTools.isNotBlank(deviceSn)){
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("deviceSn",deviceSn);
            conditions.asc("slotNum");
            try {
                return dao.findJoinByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<BatteryCabinetSlot> findByDeviceSn(List<String> deviceSnList){
        if(deviceSnList != null && deviceSnList.size() > 0){
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().in("deviceSn",deviceSnList);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public BatteryCabinetSlot getOrAddByKeys(String deviceSn,Integer slotNum) {
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("deviceSn", deviceSn);
            conditions.and().is("slotNum", slotNum);

            try {
                BatteryCabinetSlot slot = dao.get(conditions);
                if (slot != null) {
                    return slot;
                }else{
                    slot = new BatteryCabinetSlot();
                    slot.setDeviceSn(deviceSn);
                    slot.setSlotNum(slotNum);
                    slot.setCreateDate(new Date());
                    if (dao.insert(slot) > 0) {
                        return slot;
                    }
                }
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public boolean add(List<BatteryCabinetSlot> batteryCabinetSlotList){
        if(batteryCabinetSlotList != null && batteryCabinetSlotList.size() > 0) {
            try {
                return dao.insert(batteryCabinetSlotList) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public BatteryCabinetSlot getByKeys(String deviceSn,Integer slotNum){
        if(StringTools.isNotBlank(deviceSn) && slotNum != null){
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("deviceSn",deviceSn);
            conditions.and().is("slotNum",slotNum);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean updateNotNull(BatteryCabinetSlot batteryCabinetSlot){
        if(StringTools.isNotBlank(batteryCabinetSlot.getDeviceSn()) && batteryCabinetSlot.getSlotNum() != null) {
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("deviceSn", batteryCabinetSlot.getDeviceSn());
            conditions.and().is("slotNum", batteryCabinetSlot.getSlotNum());
            try {
                return dao.updateNotNull(batteryCabinetSlot, conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean removePortableBatterySn(String portableBatterySn){
        if(StringTools.isNotBlank(portableBatterySn)) {
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("portableBatterySn", portableBatterySn);

            Map<String,Object> data = new HashMap<>();
            data.put("portableBatterySn",null);

            try {
                return dao.update(data, conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean removePortableBatterySn(String deviceSn,Integer slotNum){
        if(StringTools.isNotBlank(deviceSn) && slotNum != null) {
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("deviceSn", deviceSn);
            conditions.and().is("slotNum", slotNum);

            Map<String,Object> data = new HashMap<>();
            data.put("portableBatterySn",null);

            try {
                return dao.update(data, conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByDeviceSn(String deviceSn){
        if(StringTools.isNotBlank(deviceSn)) {
            Conditions conditions = new Conditions(BatteryCabinetSlot.class);
            conditions.and().is("deviceSn", deviceSn);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
}
